// pages/user/bind1/bind.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    help_status: false,
    userName: '',
    password: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  useridInput: function (e) {
    if (e.detail.value.length >= 18) {
      wx.hideKeyboard();
    }
  },
  tapHelp: function (e) {
    if (e.target.id == 'help') {
      this.hideHelp();
    }
  },
/**
   * 点击帮助 执行
   */
  showHelp: function (e) {
    this.setData({
      'help_status': true
    });
  },
/**
   * 关闭 帮助
   */
  hideHelp: function (e) {
    this.setData({
      'help_status': false
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  formSubmit: function (e) {
    //console.log('111')
    //let _this = this
    let uname = e.detail.value.userName.replace(/\s+/g,'');
    let upwd = e.detail.value.password.replace(/\s+/g,'');
    //console.log('输入的账号为==：'+uname)
    //console.log('输入的密码为=='+upwd)
    //账号为空验证
    if (uname == null || uname == '') {
      wx.showModal({
        title: '请输入您的账号！'
      })
      return
    }
    //手机号码格式正则表达式
    if(uname.length == 11){
      let phonetel = /^1[0-9]{10}$/;
      if (!phonetel.test(uname)) {
        wx.showModal({
          title: '手机号码格式有误!'
        })
        return
      }
    }
    //密码为空验证
    if (upwd == null || upwd == '') {
      wx.showModal({
        title: '请输入密码！'
      })
      return
    }
    //缓存读取数据，判断是否用户授权
    try {
      var value = wx.getStorageSync('wxUserInfo')
      if (value) {
        console.log('获取到用户信息')
      }else{
        //console.log('1111')
        const accountInfo = wx.getAccountInfoSync()
        wx.login({
          success(wxres) {
            if (wxres.code) {
              app.formPost('/wx/user/binding', {
                        'umobile': uname,
                        'upwd': upwd,
                        "code": wxres.code,
                        'appid': accountInfo.miniProgram.appId
                }).then(res =>{
                    if(res.code == 200){
                      wx.setStorageSync('token', res.token)
                      wx.setStorageSync('empinfo', res.empinfo)
                      wx.setStorageSync('subscribe', res.subscribe)
                      wx.reLaunch({
                                    url: '/pages/tiku/fenlei',
                                  })
                    }else if(res.code == 100){
                         //wx.setStorageSync('subscribe', res.subscribe)
                              // if(!res.subscribe){
                              //     _this.setData({
                              //       showSubscribe: true
                              //     });
                              //   }
                                //console.log(showSubscribe)
                                wx.showModal({
                                  title: res.msg
                                })
                   }
                }).catch(e => {
                  console.log(e)
                  wx.showModal({title: e})
                })
            } else {
              wx.showModal({
                title: wxres.errMsg
              })
            }
          }
        })
      }
    } catch (e) {
      wx.showModal({
        title: '网络异常，请稍后再试!'
      });
      wx.reLaunch({
        url: '/pages/user/bind/index',
      });
    }
  }
})